package com.hzqy.web.ac;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.commons.utils.ConstantUtils;
import com.hzqy.service.warn.WarnServiceBean;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.SysWarnVo;
import com.hzqy.web.vo.content.CmContentLedgerVo;

@Controller
@RequestMapping("/")
public class WarnAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	private final String picTypeWarn = "公告";
	private final String picTypeContent = "内容";
	private final String picTypeSeries = "剧集";
	private final String picTypeModel = "模板";
	
	@Resource
	private WarnServiceBean warnServiceBean;

	@RequestMapping("/warn/select_warn")
	@ResponseBody
	public Object selectSysWarn(String f_sw_id) {
		SysWarnVo sysWarnVo = warnServiceBean.selectSysWarn(f_sw_id);
		return sysWarnVo;
	}
	
	@RequestMapping("/warn/warn_list")
	@ResponseBody
	public Object warnList(ModelMap map,SysWarnVo sysWarnVo, HttpSession session, int page, int rows) {
		Map<String,Object> warnList = new HashMap<String, Object>();
		warnList =  warnServiceBean.selectSysWarnList(sysWarnVo, page, rows);
		return warnList;
	}
	
	@RequestMapping("/warn/insert_warn")
	@ResponseBody
	public Object insertSysWarn(SysWarnVo sysWarnVo,String picJson, HttpSession session) throws UnsupportedEncodingException {
		Base64.Decoder decoder = Base64.getDecoder();
		byte[] bytes = decoder.decode(sysWarnVo.getF_sw_desc());
		String F_sw_desc_decode = new String(bytes, "UTF-8");
		sysWarnVo.setF_sw_desc(F_sw_desc_decode);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap = warnServiceBean.insertSysWarn(sysWarnVo,picJson);
		return resultMap;
	}
	
	@RequestMapping("/warn/delete_warn")
	@ResponseBody
	public Object deleteWarn(String f_sw_id, HttpSession session) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = warnServiceBean.deleteSysWarn(f_sw_id);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","删除失败!");
		}
		return resultMap;
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/warn/batch_delete_warn")
	@ResponseBody
	public Object batchDeleteWarn(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = warnServiceBean.batchDeleteWarn(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量删除失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/warn/update_warn")
	@ResponseBody
	public Object updateSysWarn( HttpSession session,SysWarnVo sysWarnVo , String picJson ) throws UnsupportedEncodingException {
		Base64.Decoder decoder = Base64.getDecoder();
		byte[] bytes = decoder.decode(sysWarnVo.getF_sw_desc());
		String F_sw_desc_decode = new String(bytes, "UTF-8");
		sysWarnVo.setF_sw_desc(F_sw_desc_decode);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		String picType=picTypeWarn;
		resultMap = warnServiceBean.updateSysWarn(sysWarnVo,picJson,picType);
		return resultMap;
	}
	
	/**
	 * 批量生效
	 * @param ids
	 * @return
	 */
	@RequestMapping("/warn/batch_review_warn")
	@ResponseBody
	public Object batchReviewWarn(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = warnServiceBean.batchReviewSysWarn(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量生效成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量生效失败!flag="+flag);
		}
		return resultMap;
	}
	
	/**
	 * 批量下线
	 * @param ids
	 * @return
	 */
	@RequestMapping("/warn/batch_unreview_warn")
	@ResponseBody
	public Object batchUnReviewWarn(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = warnServiceBean.batchUnReviewSysWarn(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量失效成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量失效失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/warn/update_warn_status")
	@ResponseBody
	public Object updateWarnStatus(String f_cc_id,String f_cc_status) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = warnServiceBean.updateSysWarnStatus(f_cc_id,f_cc_status);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","修改成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","修改失败!");
		}
		return resultMap;
	}
	
//	@RequestMapping("/warn/review_warn")
//	@ResponseBody
//	public Object reviewWarn( HttpSession session,SysWarnVo sysWarnVo) {
//		Map<String,Object> resultMap = new HashMap<String,Object>();
//		int flag = warnServiceBean.reviewWarnVo(sysWarnVo);
//		if(flag > 0) {
//			resultMap.put("success", true);
//			resultMap.put("result","审核成功!");
//		} else {
//			resultMap.put("success", false);
//			if(flag==-4){
//				resultMap.put("result","审核失败,无该审核状态!flag="+flag);
//			}else if(flag==-3){
//				resultMap.put("result","审核失败,无审核权限!flag="+flag);
//			}else if(flag==0){
//				resultMap.put("result","审核失败!flag="+flag);
//			}else if(flag==-10){
//				resultMap.put("result","审核失败,模板存在危害平台代码!flag="+flag);
//			}else if(flag==-9){
//				resultMap.put("result","审核失败,模板解压失败!flag="+flag);
//			}else{
//				resultMap.put("result","审核失败!flag="+flag);
//			}
//			
//		}
//		return resultMap;
//	}
}
